/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chameleon.model.game;

import chameleon.gameSolver.NuGat.NuGatInterface;
import chameleon.model.NuGatStructure.symbTable.SymbLayer;
import chameleon.model.NuGatStructure.symbTable.SymbTable;
import chameleon.model.transitionFormat.AbstractTransition;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author simon
 */
public class Player {
    
    private int player_; //1 or 2
    private Vector<Variable> variables_;
    private Vector<AbstractTransition> transitions_;
    private String layerName_;
    
    public Player (int player, Vector<Variable> variable, 
            Vector<AbstractTransition> transitions ){
        if (player == 1 || player == 2){
            player_ = player;
            variables_ = (Vector<Variable>) variable.clone();
            //transitions_ = (Vector<AbstractTransition>) transitions.clone();
            if (player_ == 1)
                layerName_ = "layer_of_PLAYER_1";
            else
                layerName_ = "layer_of_PLAYER_2";
        }
        else
            throw new RuntimeException("Error during instanciation of a player."
                    + " Only two player games are supported");
    }
    
    public NuGatInterface addPlayer(NuGatInterface nugat, Game game){
        
        //Add the variables in the database
        
        SymbTable st = game.getGlobalSymbTable();
        
        //First remove the previous version of the layer
        /*SymbLayer layer = st.getLayer(layerName_);        
        game.getBoolEncoding().removeLayer(layer);
        game.getBddEncoding().removeLayer(layer);
        st.removeLayer(layerName_);*/
        
        st.createLayer("test", nugat.SYMB_LAYER_POS_BOTTOM);
        
        Iterator iter = variables_.iterator();
        while(iter.hasNext()){
            Variable var = (Variable) iter.next();
            st.addIntVariable(var.getName(), player_, var.getLowerBound(), 
                    var.getUpperBound());
        }
        
        //Committing the new layer
        game.getBoolEncoding().commitLayer(st.getLayer("test"));
        game.getBddEncoding().commitLayer(st.getLayer("test"));
        
        return nugat;
    }
    
}
